package com.authenticateCenter.controller;

import com.authenticateCenter.domain.dto.AuthorityDto;
import com.authenticateCenter.domain.entity.Authority;
import com.authenticateCenter.domain.vo.AuthorityVo;
import com.authenticateCenter.service.IAuthorityService;
import com.authenticateCenter.service.IRoleAuthorityService;
import com.authenticateCenter.util.StringUtil;
import com.common.mybaits.CustomPage;
import com.common.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import static com.authenticateCenter.factory.AuthorityFactory.AUTHORITY_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/authority")
@RequiredArgsConstructor
@Api(tags = "资源权限管理")
@ResponseResultAdvice
public class AuthorityController {
    private final IAuthorityService iAuthorityService;
    private final IRoleAuthorityService iRoleAuthorityService;

    @ApiOperation("添加权限")
    @PostMapping("/add")
    public Integer addAuthority(@RequestBody@Valid AuthorityDto authorityDto){
        Authority authority = AUTHORITY_CONVERT.toAuthority(authorityDto);
        authority.insertCheck(iAuthorityService);
        iAuthorityService.save(authority);
        return authority.getId();
    }

    @ApiOperation("删除权限")
    @DeleteMapping("/delete")
    public void deleteAuthority(@RequestParam("id")Integer authorityId){
        iAuthorityService.removeAuthority(authorityId,iRoleAuthorityService);
    }

    @ApiOperation("返回已经添加的所有权限")
    @GetMapping("/list")
    public List<AuthorityVo> getAuthorityList(CustomPage customPage){
        List<Authority> authorityList = CustomPage.getResult(customPage, new Authority(), iAuthorityService, null);
        return authorityList.stream().map(authority -> AUTHORITY_CONVERT.toAuthorityVo(authority)).collect(Collectors.toList());
    }

    @ApiOperation("返回某个角色具有的权限集合")
    @GetMapping("/listOfRole")
    public List<AuthorityVo> getAuthorityListOfRole(@RequestParam("roleId")Integer roleId,CustomPage customPage){
        return iAuthorityService.getAuthoritysByRid(roleId,customPage).stream().map(authority -> AUTHORITY_CONVERT.toAuthorityVo(authority)).collect(Collectors.toList());
    }

    @ApiOperation("发放权限给某个角色")
    @PostMapping("/release")
    public void releaseAuthorityToRole(@RequestParam("roleId")Integer roleId,@ApiParam("authorityId按照英文逗号分隔") @RequestParam("authorityIdList")String authorityIdList){
        iRoleAuthorityService.releaseAuthorityToRole(roleId, StringUtil.strIdToListInt(authorityIdList));
    }

    @ApiOperation("收回某个角色的相关权限")
    @PostMapping("/revoke")
    public void revokeRoleOfAuthority(@RequestParam("roleId")Integer roleId,@ApiParam("authorityId按照英文逗号分隔")@RequestParam("authorityIdList")String authorityIdList){
        iRoleAuthorityService.revokeRoleOfAuthority(roleId,StringUtil.strIdToListInt(authorityIdList));
    }
}
